(function() {
    angular
        .module('ydApp.user', [])
        .config(function($stateProvider) {
            $stateProvider.state('tabs.user', {
                url: '/user-center',
                cache: false,
                nativeTransitions: null,
                views: {
                    'user-tab': {
                        controller: "userCenterCtrl",
                        templateUrl: 'views/userCenter/index-user.html'
                    }
                }
            });
            $stateProvider.state('tabs.user-doc', {
                cache: false,
                url: '/doc-center',
                views: {
                    'user-tab': {
                        controller: "docCenterCtrl",
                        templateUrl: 'views/userCenter/index-doc.html'
                    }
                }
            });
        })
        .controller("userCenterCtrl", userCenterCtrl)
        .controller("docCenterCtrl", docCenterCtrl);

    function userCenterCtrl($scope, $rootScope, $state, localStore, Cameranav, fileTransfer, $ionicLoading, xhr) {
        var vm = $scope.vm = {};
        vm.user = localStore.get('user');
        console.log(vm.user.headImgUrl)

        $scope.getAvatar = function() {
            Cameranav.getPicture(function(imgUri) {
                $ionicLoading.show({
                    template: '正在上传…'
                });
                fileTransfer.upload(imgUri, vm.user.id, function(data) {
                    if (data.successful) {
                        vm.user.headImgUrl = data.data.picUrl;

                        if (localStore.get("userType") == 1) {
                            url = "HealthInquiry/com/user/updateUserInfo";
                        } else {
                            url = "HealthInquiry/com/user/update"
                        }
                        xhr.get(url, vm.user).then(function(result) {
                            localStore.save("user", vm.user);
                            $ionicLoading.hide();
                        }).then(function(result) {
                            $ionicLoading.show({
                                template: '上传失败'
                            });
                            $ionicLoading.hide();
                        });

                    }
                    else{
                        $ionicLoading.hide();
                    }
                })
            })
        }
    }


    function docCenterCtrl($scope, $state, localStore) {
        var vm = $scope.vm = {};
        vm.user = localStore.get('user');
    }

})();
